#!/bin/bash
echo "🔧 开始一键编译 cbp2make_ai..."

# 创建目录
mkdir -p bin .objs

# 修复 WriteCallback 函数 - 检查并添加到 unified_ai_client.cpp
if ! grep -q "WriteCallback" src/unified_ai_client.cpp; then
    echo "📝 添加 WriteCallback 函数到 unified_ai_client.cpp..."
    cat >> src/unified_ai_client.cpp << 'WRITECALLBACK'

// WriteCallback 函数 - 用于 libcurl 数据接收
static size_t WriteCallback(void* contents, size_t size, size_t nmemb, std::string* response) {
    size_t totalSize = size * nmemb;
    response->append((char*)contents, totalSize);
    return totalSize;
}
WRITECALLBACK
    echo "✅ WriteCallback 函数添加成功"
else
    echo "✅ WriteCallback 函数已存在"
fi

# 设置编译参数
INCLUDES="-I../cbp2make/lib -I../cbp2make/lib/tinyxml -I../cbp2make/src -Ilib -Isrc"
CXXFLAGS="-std=c++11 -DAICODER_ENHANCED -m64 -O2"

echo "📦 编译基础库文件..."
# 编译 tinyxml 库
g++ $INCLUDES $CXXFLAGS -c lib/tinyxml.cpp -o .objs/tinyxml.o
g++ $INCLUDES $CXXFLAGS -c lib/tinystr.cpp -o .objs/tinystr.o
g++ $INCLUDES $CXXFLAGS -c lib/tinyxmlerror.cpp -o .objs/tinyxmlerror.o
g++ $INCLUDES $CXXFLAGS -c lib/tinyxmlparser.cpp -o .objs/tinyxmlparser.o

echo "📦 编译核心文件..."
# 编译核心文件
g++ $INCLUDES $CXXFLAGS -c src/cbp2make.cpp -o .objs/cbp2make.o
g++ $INCLUDES $CXXFLAGS -c src/stlconvert.cpp -o .objs/stlconvert.o
g++ $INCLUDES $CXXFLAGS -c src/stlstrings.cpp -o .objs/stlstrings.o
g++ $INCLUDES $CXXFLAGS -c src/stlvariables.cpp -o .objs/stlvariables.o
g++ $INCLUDES $CXXFLAGS -c src/stlconfig.cpp -o .objs/stlconfig.o
g++ $INCLUDES $CXXFLAGS -c src/stlfutils.cpp -o .objs/stlfutils.o
g++ $INCLUDES $CXXFLAGS -c src/stringhash.cpp -o .objs/stringhash.o
g++ $INCLUDES $CXXFLAGS -c src/cbproject.cpp -o .objs/cbproject.o
g++ $INCLUDES $CXXFLAGS -c src/cbptarget.cpp -o .objs/cbptarget.o
g++ $INCLUDES $CXXFLAGS -c src/cbworkspace.cpp -o .objs/cbworkspace.o
g++ $INCLUDES $CXXFLAGS -c src/cbbuildcfg.cpp -o .objs/cbbuildcfg.o
g++ $INCLUDES $CXXFLAGS -c src/cbbuildmgr.cpp -o .objs/cbbuildmgr.o
g++ $INCLUDES $CXXFLAGS -c src/cbglobalvar.cpp -o .objs/cbglobalvar.o
g++ $INCLUDES $CXXFLAGS -c src/cbhelper.cpp -o .objs/cbhelper.o
g++ $INCLUDES $CXXFLAGS -c src/cbpunit.cpp -o .objs/cbpunit.o
g++ $INCLUDES $CXXFLAGS -c src/depsearch.cpp -o .objs/depsearch.o
g++ $INCLUDES $CXXFLAGS -c src/makefile.cpp -o .objs/makefile.o
g++ $INCLUDES $CXXFLAGS -c src/platforms.cpp -o .objs/platforms.o
g++ $INCLUDES $CXXFLAGS -c src/toolchains.cpp -o .objs/toolchains.o
g++ $INCLUDES $CXXFLAGS -c src/buildtools.cpp -o .objs/buildtools.o
g++ $INCLUDES $CXXFLAGS -c src/stlgpm.cpp -o .objs/stlgpm.o

echo "🤖 编译AI功能文件..."
# 编译AI功能文件
g++ $INCLUDES $CXXFLAGS -c src/ai_model_config.cpp -o .objs/ai_model_config.o
g++ $INCLUDES $CXXFLAGS -c src/unified_ai_client.cpp -o .objs/unified_ai_client.o
g++ $INCLUDES $CXXFLAGS -c src/aicoder_enhancements.cpp -o .objs/aicoder_enhancements.o

echo "🔗 链接生成可执行文件..."
# 链接所有对象文件
g++ -m64 .objs/*.o -lcurl -o bin/cbp2make_ai.exe

# 检查是否成功
if [ -f "bin/cbp2make_ai.exe" ]; then
    echo "🎉 编译成功！生成文件: bin/cbp2make_ai.exe"
    echo "📋 测试AI模型设置功能:"
    echo "   ./bin/cbp2make_ai.exe --ai-set-model 0 DS-V3.2exp0922"
    echo "   ./bin/cbp2make_ai.exe --ai-set-model 1 UE-CodeExpert"
    echo "   ./bin/cbp2make_ai.exe --ai-set-model 2 GPT-4"
else
    echo "❌ 编译失败，请检查错误信息"
    exit 1
fi
